package com.ho1ho.audioexample.tcp;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class FirstIntLeDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        int bufLen = in.readableBytes();
        if (bufLen < 4) {
            return;
        }
        in.markReaderIndex();
        int dataLen = in.readInt();

        if (in.readableBytes() < dataLen) {
            in.resetReaderIndex();
            return;
        }

        out.add(in.readBytes(dataLen));
    }
}
